package org.codeblue.support.utils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by jl.hu on 2017/4/7.
 */
public class Mvcs {


    private static final ThreadLocal<HttpServletRequest> REQUEST_THREAD_LOCAL = new ThreadLocal<HttpServletRequest>();

    private static final ThreadLocal<HttpServletResponse> RESPONSE_THREAD_LOCAL = new ThreadLocal<HttpServletResponse>();

    private static final ThreadLocal<Object> HANDLER_THREAD_LOCAL = new ThreadLocal<Object>();

    public static void bind(ServletRequest request, ServletResponse response) {
        setRequest(request);
        setResponse(response);
    }

    public static void setRequest(ServletRequest request) {
        REQUEST_THREAD_LOCAL.set((HttpServletRequest) request);
    }

    public static void setResponse(ServletResponse response) {
        RESPONSE_THREAD_LOCAL.set((HttpServletResponse) response);
    }

    public static HttpServletRequest getRequest() {
        HttpServletRequest request = REQUEST_THREAD_LOCAL.get();
        if (null == request) {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (null != requestAttributes) {
                if (requestAttributes instanceof HttpServletRequest) {
                    request = (HttpServletRequest) requestAttributes;
                } else if (requestAttributes instanceof ServletRequestAttributes) {
                    request = ((ServletRequestAttributes) requestAttributes).getRequest();
                }
            }

            if (null == request) {
                //  log.warn("getRequest() returns null, requestAttributes=" + requestAttributes);
            }
        }
        return request;
    }

    public static HttpServletResponse getResponse() {
        return RESPONSE_THREAD_LOCAL.get();
    }

    public static PageBounds  getPageBounds(){
        Integer pageIndex = getRequest().getParameter("pageSize")  == null ?  10:  Integer.valueOf(getRequest().getParameter("pageSize")) ;
        Integer pageSize  = getRequest().getParameter("pageIndex") == null ? 0:   Integer.valueOf(getRequest().getParameter("pageIndex")) ;
        return new PageBounds(pageIndex,pageSize );
    }

    public static void printJson(String text) {
        try {
            HttpServletResponse response = getResponse();
            response.addHeader("Content-Type", "application/json;charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print(text);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
